package hoowin.basic.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validator {
	private static final String emailAddressPattern =
    	"\\b(^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b";
	private static final String phoneNumberPattern =
		"^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4,8}$";
	
	public static boolean isEmailAddress(String email, boolean trim){
		return match(emailAddressPattern, email, trim);
	}
	public static boolean isPhoneNumber(String phone, boolean trim){
		return match(phoneNumberPattern, phone, trim);
	}
	private static boolean match(String expression, String text, boolean trim){
		if(text==null || text.length()==0)return false;
		Pattern pattern;
		if(trim)text = text.trim();
		pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(text);
		return matcher.matches();
	}
}